package com.hm.service.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.service.wechat.IWeChatPublic;
import jakarta.servlet.http.HttpServletRequest;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
@RequestMapping(value = "/MAIN", produces = {"application/json;charset=UTF-8"})
public class WCPS_000_000_004 {
    private static final Logger logger = LoggerFactory.getLogger(WCPS_000_000_004.class);


    @Qualifier("OkHttpClient")
    public OkHttpClient okHttpClient;

    private final IWeChatPublic iWeChatPublic;

    public WCPS_000_000_004(IWeChatPublic iWeChatPublic, OkHttpClient okHttpClient) {
        this.iWeChatPublic = iWeChatPublic;
        this.okHttpClient = okHttpClient;
    }


    //逆地理编码
    @PostMapping(value = "/000_000_004")
    public String doPost(HttpServletRequest request) {
        try {
            String body = request.getReader().readLine();
            JSONObject reqObject = JSONObject.parseObject(body);
            String longitude = reqObject.getString("longitude");//维度
            String latitude = reqObject.getString("latitude");//经度

            //get请求
            String reqUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + "," + longitude + "&key=TG7BZ-JMEC4-U5LUG-FIXNM-6HZLE-3IBG5";
            Request build = new Request.Builder()//
                    .url(reqUrl)//
                    .build();//
            try (Response response = okHttpClient.newCall(build).execute()) {
                ResponseBody resBody = response.body();
                if (resBody != null) {
                    String jsonString = resBody.string();
                    logger.error("逆地理编码成功:" + jsonString);
                    return jsonString;
                } else {
                    logger.error("逆地理编码成功获取resBody为null！！ ");
                }
            } catch (Exception e) {
                logger.error("逆地理编码异常：Exception: " + e.getMessage());
            }

            return "1";

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return "";
    }


}
